package tr.edu.yildiz.ce.dap.apm.services.impl;

import java.util.List;

import tr.edu.yildiz.ce.dap.acm.service.CacheManagerService;
import tr.edu.yildiz.ce.dap.acm.service.impl.CacheManagerServiceImpl;
import tr.edu.yildiz.ce.dap.acm.util.exception.CacheCanNotBeFoundException;
import tr.edu.yildiz.ce.dap.acm.util.exception.CacheConfigurationFaultException;
import tr.edu.yildiz.ce.dap.acm.util.exception.CanNotGenerateIDException;
import tr.edu.yildiz.ce.dap.apm.ProxyProperties;
import tr.edu.yildiz.ce.dap.apm.services.ApplicationService;
import tr.edu.yildiz.ce.dap.apm.util.AppCache;
import tr.edu.yildiz.ce.dap.appagent.criteria.CriteriaConstants.NAME;
import tr.edu.yildiz.ce.dap.appagent.criteria.CriteriaConstants.OPERATOR;
import tr.edu.yildiz.ce.dap.appagent.model.App;
import tr.edu.yildiz.ce.dap.appagent.model.AppDefinition;
import tr.edu.yildiz.ce.dap.appagent.model.AppList;
import tr.edu.yildiz.ce.dap.appagent.model.Component;
import tr.edu.yildiz.ce.dap.appagent.model.ComponentList;
import tr.edu.yildiz.ce.dap.appagent.model.Criteria;
import tr.edu.yildiz.ce.dap.appagent.model.CriteriaList;

public class ApplicationServiceImpl implements ApplicationService{
	
	private boolean applyCriteria(App app,CriteriaList criterias){
		for (Criteria c : criterias.getCriteria()) {
			if (c.getName().equals(NAME.APP_NAME)){
				if (c.getOperator().equals(OPERATOR.EQUALS))
					if (!app.getName().equals(c.getValue()))
						return false;
				if (c.getOperator().equals(OPERATOR.LIKE))
					if (!app.getName().contains(c.getValue()))
						return false;
			}
			if (c.getName().equals(NAME.APP_VERSION)){
				if (c.getOperator().equals(OPERATOR.EQUALS))
					if (!app.getVersion().equals(c.getValue()))
						return false;
				if (c.getOperator().equals(OPERATOR.LIKE))
					if (!app.getVersion().contains(c.getValue()))
						return false;
				if (c.getOperator().equals(OPERATOR.GREATER_THAN))
					if (app.getVersion().compareTo(c.getValue())> 0)
						return false;
				if (c.getOperator().equals(OPERATOR.LESS_THAN))
					if (app.getVersion().compareTo(c.getValue())< 0)
						return false;
			}
		}
		return true;
	}

	public AppList listAll() {
		List<AppDefinition> appDefList = getAllAppDefList();
		AppList appList = new AppList();
		for (AppDefinition appDefinition : appDefList) {
			appList.getApp().add(appDefinition.getApp());
		}
		return appList;
	}

	private List<AppDefinition> getAllAppDefList() {
		List<AppDefinition> appDefList = AppCache.getInstance().getAppDefList();
		appDefList.addAll(AppCache.getInstance().getAutoAppDefList());
		return appDefList;
	}

	public AppList list(CriteriaList criterias) {
		List<AppDefinition> appDefList = getAllAppDefList();
		AppList appList = new AppList();
		for (AppDefinition appDefinition : appDefList) {
			if (applyCriteria(appDefinition.getApp(), criterias))
				appList.getApp().add(appDefinition.getApp());
		}
		return appList;
	}

	public ComponentList getDetail(String name, String version) {
		List<AppDefinition> appDefList = getAllAppDefList();
		for (AppDefinition appDefinition : appDefList) {
			if (appDefinition.getApp().getName().equals(name) 
					&& appDefinition.getApp().getVersion().equals(version))
				return appDefinition.getComponentList();
		}
		return new ComponentList();
	}
	
	public String register(String name, int port) {
		// TODO Auto-generated method stub
		return "00";
	}

}
